﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kevin.Comm.Utility;
using Kevin.Comm.Web;
using Kevin.Shop.Business.Core.Components.Responser;
using Kevin.Shop.Business.Core.Components.Sales;
using Kevin.Shop.Business.SalesManagement.Components.Order;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Shopping.Components;
using RiderShop.Mobile.Modle.Purchase;
using RiderShop.Mobile.Common.Component;

namespace RiderShop.WebSite.Controllers.Purchase
{
    public partial class PurchaseController : ThemeController
    {

        public ActionResult ShoppingCart(string act, string pId, string qty)
        {
            var model = new ShippingCartModel();
            model.UpdataShippingCart(act, pId, qty);
            return View(WebClientContext.Current.SiteTheme, "Purchase", "ShoppingCart", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
        }

        public JsonResult PreCheckProduct()
        {
            var model = new ShippingCartModel();
            var result = model.PreCheckProduct();
            return Json(new ServiceResponser(result == string.Empty, result));
        }

        public JsonResult GetShippingCartItems()
        {
            return Json(ShippingCartModel.BuildShoppingCartPro());
        }

        public JsonResult GetShippingCartInfo()
        {
            var model = new ShippingCartModel();
            var result = model.GetShippingCartInfo();
            return Json(result);
        }


        [Authorize]
        public ActionResult CheckOut()
        {
            var model = new CheckOutModel();
            //return View(model);
            return View(WebClientContext.Current.SiteTheme, "Purchase", "CheckOut", WebClientContext.Current.CacheKeyDefaultMasterPage, model);
        }

        [Authorize]
        public ActionResult SubmitOrder()
        {
            string strJson = WebClientContext.ReadJsonStringFromRequestBody(Request.InputStream);
            string str = WebHelper.UrlDecode(strJson).Replace("\r\n", @" ");
            var order = Serializer.ConvertJsonToObject<OrderBasicInfo>(str, true);
            order.OrderSource = OrderSourceType.PC;
            var model = new CheckOutModel();
            var result = model.SubmitOrder(order);
            if (result.Status)
            {
                ShoppingCartHelper.ProcessShoppingCart("clear", string.Empty, string.Empty);
            }
            else
            {
                return Json(result);
            }
            return Json(result);
        }

        [Authorize]
        public ActionResult OrderCompleted()
        {
            var tag = Request.QueryString["par"];
            return View(WebClientContext.Current.SiteTheme, "Purchase", "OrderCompleted", WebClientContext.Current.CacheKeyDefaultMasterPage, new OrderCompletedModel(tag));
        }

        [Authorize]
        public JsonResult OrderBalanceCompute(int shiptype, int shippingId)
        {
            var model = new CheckOutModel();
            var result = model.OrderBalanceCompute(shiptype, shippingId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public ActionResult PaymentCompleted()
        {
            return View(WebClientContext.Current.SiteTheme, "Purchase", "PaymentCompleted", WebClientContext.Current.CacheKeyDefaultMasterPage, null);
        }
    }
}